var grid;
var iTrainings = 1;

$('document').ready(function () {
    var dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: appBosch.read,
                dataType: "json"
            },
            update: {
                url: appBosch.update,
                dataType: "json"
            },
            parameterMap: function (options, operation) {
                var params = {};
                switch (operation)
                {
                    case 'update':
                        params = {models: kendo.stringify(options.models[0])};
                        break;
                }
                return params;
            }
        },
        batch: true,
        schema: {
            model: {
                id: "value",
                fields: {
                    nomina: {
                        type: 'string',
                        editable: false
                    },
                    name: {
                        type: 'string',
                        editable: false
                    },
                    area: {
                        type: 'string',
                        editable: false
                    },
                    line: {
                        type: 'string',
                        editable: false
                    },
                    workstation: {
                        type: 'string',
                        editable: false
                    },
                    end: {
                        type: 'date',
                        required: true
                    },
                    min_end: {
                        type: 'date',
                        editable: false
                    },
                    max_end: {
                        type: 'date',
                        editable: false
                    }
                }
            }
        }
    });

    grid = $("#grid").kendoGrid({
        toolbar: [
            {
                name: "pdf",
                text: "Exportar a PDF"
            },
            {
                name: "excel",
                text: "Exportar a Excel"
            }
        ],
        excel: {
            fileName: "Certificaciones en entrenamiento próximas a terminar.xlsx",
            filterable: true,
            allPages: true
        },
        pdf: {
            allPages: true,
            fileName: "Certificaciones en entrenamiento próximas a terminar.pdf"
        },
        scrollable: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        resizable: true,
        columnMenu: true,
        sortable: true,
        dataSource: dataSource,
        dataBound: function () {
            iTrainings = 1;
        },
        groupable: true,
        columns: [
            {
                title: "#",
                template: '#=iTrainings++#',
                width:50
            },
            {
                field: "nomina",
                title: "Nómina"
            },
            {
                field: "name",
                title: "Asociado",
                hidden: true
            },
            {
                field: "area",
                title: "Área"
            },
            {
                field: "line",
                title: "Línea"
            },
            {
                field: "workstation",
                title: "Estación"
            },
            {
                field: "end",
                title: "Fin del entrenamiento",
                template: '<span title="#=kendo.toString(end, "D")#" >#=kendo.toString(end, "d")#</span>',
                editor: dateEditor
            },
            {
                command: [
                    {
                        name: "edit",
                        text: {
                            edit: "",
                            update: "",
                            cancel: ""
                        }
                    }
                ],
                width: 100
            }
        ],
        editable: "inline"
    }).data('kendoGrid');
});

function dateEditor(container, options) {
    var input = $('<input />');
    input.attr("name", options.field);
    input.appendTo(container);
    input = input.kendoDatePicker({
        format: "d",
        parseFormats: [ 'yyyy-MM-dd' ],
        min: options.model.min_end,
        max: options.model.max_end,
        value: options.model.end
    }).data('kendoDatePicker');
}